package com.example.service.impl;

import com.example.dao.ServiceApplicationDao;
import com.example.entity.ServiceApplication;
import com.example.service.ServiceApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ServiceApplicationServiceImpl implements ServiceApplicationService {

    @Autowired
    private ServiceApplicationDao applicationDao;

    @Override
    public boolean submitApplication(ServiceApplication application) {
        application.setStatus("PENDING");
        application.setSubmitted_at(new Date());
        return applicationDao.insertApplication(application) > 0;
    }

    @Override
    public boolean reviewApplication(Integer applicationId, Integer reviewerId, String status, String comment) {
        ServiceApplication application = applicationDao.getApplicationById(applicationId);
        if (application == null) return false;

        application.setStatus(status);
        application.setReviewer_id(reviewerId);
        application.setReview_comment(comment);
        application.setReviewed_at(new Date());

        return applicationDao.updateApplication(application) > 0;
    }

    @Override
    public List<ServiceApplication> getPendingApplications() {
        return applicationDao.getPendingApplications();
    }

    @Override
    public List<ServiceApplication> getUserApplications(Integer userId) {
        return applicationDao.getApplicationsByUser(userId);
    }

    @Override
    public ServiceApplication getApplicationById(Integer applicationId) {
        return applicationDao.getApplicationById(applicationId);
    }
    @Override
    public ServiceApplication getLatestApprovedApplication(Integer userId, String applicationType) {
        return applicationDao.getLatestApprovedApplication(userId, applicationType);
    }

}